<?php
use EasyWeChat\Factory;
use jianyan\excel\Excel;
require_once("./vendor/autoload.php");
require_once("./lib/fun.php");
require_once("./lib/pdo.php");


$param = I("param.");
$token = str_replace(" ","+",I("token"));
$uid = authcode($token,'DECODE',"youluda",7*24*3600);
$check_status = I("param.check");
if(!$uid){
    json([
        'code' => 2,
        'msg'  => '请先登录一下',
    ]);
}

$where  = [
    'uid' => $uid,
];

$db = db("lu_user");
$user_info = $db->where(['id' => $uid])->find();
if(!$user_info){
        json([
            'code' => 0,
            '获取用户信息失败'
        ]);
}

if($user_info['business_id']){
    
    $where_business = [
        'id' => $user_info['business_id'],
        ];
    $business_info = $db->where($where_business)->find();
    $business_info['is_vip'] = (int)$business_info['vip_time'] + 3600 * 24 > time() ? 1 : 0;
    $business_info['is_business_vip'] = (int)$business_info['business_time'] + 3600 * 24 > time() ? 1 : 0;
    $user_info['is_vip'] = $business_info['is_vip'];
    $user_info['is_business_vip'] = $user_info['is_business_vip'];
}else{
    $user_info['is_vip'] = (int)$user_info['vip_time'] + 3600 * 24 > time() ? 1 : 0;
    $user_info['is_business_vip'] = (int)$user_info['business_time'] + 3600 * 24 > time() ? 1 : 0;
}

$user_info['is_vip'] = (int)$user_info['vip_time'] + 3600 * 24 > time() ? 1 : 0;
$user_info['is_business_vip'] = (int)$user_info['business_time'] + 3600 * 24 > time() ? 1 : 0;
if(!$user_info['is_vip'] && !$user_info['is_business_vip']){
    json([
        'code' => 3,
        'msg' => '需要购买vip或者企业vip',
        ]);
}


if(!empty($param['group_id'])){
    $where['group_id'] = $param['group_id'];
}
$unit = db('lu_unit')->select();
$unit = returnArrByKey($unit,"id");

$model = db('lu_customer');
$list = $model->field("0 as sort_num,address_name,province,city,country,position,name,mobile,ext,lng,lat,manage,order_no,goods_no,
                        shop_info,user_no,goods_name,spec,unit,goods_num,price,total_price,fee,order_status,order_ext,store_num,store_unit")->where($where)->order("id desc")->select();
if(!$list){
    json([
        'code' => 0,
        'msg'  => '获取信息失败',
    ]);
}
if($check_status){
    json([
        'code' => 1,
        'msg'  => 'success',
    ]);
}
foreach($list as &$val){
    if($val['lng']){
        $val['lng'] = $val['lng'].",".$val['lat'];
    }
    unset($val['lat']);
    $val['sort_num'] = $k+1;
    $val['store_unit'] = isset($unit[$val['store_unit']]) ? $unit[$val['store_unit']]['name'] : "";
}
$title = [
    '地址排序','地点名称','省份','城市','乡镇','地点地址','客户名称','手机号','备注信息','精准坐标','客户经理','订单号',
    '商品编号','店铺信息','客户编号','商品名称','规格','单位','数量','单价','总价','运费',
    '订单状态','订单备注','货物数量/重量','货物单位'
];
$title = [
    ["地址排序",'sort_num'],["地点名称",'address_name'],['省份','province'],['城市','city'],['乡镇','country'],["地点地址",'position'],["客户名称",'name'],["手机号",'mobile'],["备注信息",'ext'],["精准坐标",'lng'],
    ["客户经理",'manage'],["订单号",'order_no'],["商品编号",'goods_no'],["店铺信息",'shop_info'],["客户编号",'user_no'],["商品名称",'goods_name'],
    ["规格",'spec'],["单位",'unit'],["数量",'goods_num'],["单价",'price'],["总价",'total_price'],["运费",'fee'],
    ["订单状态",'order_status'],["订单备注",'order_ext'],["货物数量/重量",'store_num'],["货物单位",'store_unit']
    ];
return Excel::exportData($list, $title, "收藏线路导出", 'xlsx');
?>